//
//  WDWorderOrderPendingVehDetailViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/9/5.
//

#import "WDWorderOrderPendingVehDetailViewController.h"
#import "WDWorderOrderPendingVehDetailViewModel.h"
#import "WDAppointTimeView.h" // 预约时间
#import "WDTaskTypeServFlowView.h" // 类型状态
#import "WDTaskDetailSectionView.h"
#import "WDWorderOrdMainVehDetailBasicView.h" // 基本信息
#import "WDWorderOrdMainVehInfoDetailView.h" // 车辆信息
#import "WDWorderOrdMainVehDetailServiceView.h" // 服务流程
#import "WDWorderOrdMainVehDetailFlowView.h"
#import "WDGoutServiceCancelView.h" // 取消流程
#import "WDServiceFinishView.h" // 完成工单

@interface WDWorderOrderPendingVehDetailViewController ()

@property (nonatomic, strong) NSMutableArray *viewList;

@property (nonatomic, strong) WDWorderOrderPendingVehDetailViewModel *viewModel;

/// 预约时间
@property (nonatomic, strong) WDAppointTimeView *appointTimeView;

/// 类型状态
@property (nonatomic, strong) WDTaskTypeServFlowView *typeFlowView;

/// 基本信息
@property (nonatomic, strong) WDTaskDetailSectionView *basicSectionView;

/// 基本信息
@property (nonatomic, strong) WDWorderOrdMainVehDetailBasicView *basicInfoView;

/// 车辆信息
@property (nonatomic, strong) WDTaskDetailSectionView *carSectionView;

/// 车辆信息
@property (nonatomic, strong) WDWorderOrdMainVehInfoDetailView *carInfoView;

/// 服务流程
@property (nonatomic, strong) WDTaskDetailSectionView *serviceSectionView;

/// 服务流程
@property (nonatomic, strong) WDWorderOrdMainVehDetailServiceView *serviceView;

/// 取消
@property (nonatomic, strong) WDGoutServiceCancelView *serviceCancelView;

/// 完成工单
@property (nonatomic, strong) WDServiceFinishView *serviceFinishView;

@end

@implementation WDWorderOrderPendingVehDetailViewController

- (void)dealloc {
    debugMethod();
    
}

- (void)initWithVar {
    [super initWithVar];
    
    _viewList = [NSMutableArray new];
}

- (void)initWithViewModel {
    _viewModel = [WDWorderOrderPendingVehDetailViewModel new];
    _viewModel.serviceModel = self.serviceModel;
    [_viewModel setupData];
}

- (void)initWithNavBar {
    [super initWithNavBar];
    
    [self initWithBackButton];
    
    self.navigationItem.title = self.viewModel.title;
}

- (void)initWithSubView {
    [super initWithSubView];
    
    [self initWithRootContent:NO];
    
    [self fromServerRequestData];
}

- (void)updateWithUI {
    // 预约时间
    [self initWithAppointTimeView];
    
    // 类型状态
    [self initWithTypeFlowView];
    
    [self initWithBasicSectionView];
    
    // 基本信息
    [self initWithBasicInfoView];
    
    // 车辆信息
    [self initWithCarSectionView];
    
    // 车辆信息
    [self initWithCarInfoView];
    
    /// 服务流程
    [self initWithServiceSectionView];
    
    // 服务流程
    [self initWithServiceView];
    
    // 维修中流程视图
    [self initWithMainFlowViews];
    
    [self initWithServiceCancelView];
    
    [self initWithServiceFinishView];
    
    if (self.viewModel.taskDetailModel.cancel) {
        self.rootContent.contentSize = CGSizeMake(kScreenW, self.serviceCancelView.bottom + 18);
    } else if (self.viewModel.taskDetailModel.tmpTaskFlows.count > 0) {
        WDWorderOrdMainVehDetailFlowView *view = self.viewList.lastObject;
        float height = view.bottom;
        if (self.serviceFinishView) {
            height = self.serviceFinishView.bottom;
        }
        self.rootContent.contentSize = CGSizeMake(kScreenW, height + 18);
    } else {
        self.rootContent.contentSize = CGSizeMake(kScreenW, self.serviceView.bottom + 18);
    }
    
//    if (self.viewModel.taskDetailModel.tmpTaskFlows.count > 0) {
//        WDWorderOrdMainVehDetailFlowView *view = self.viewList.lastObject;
//        self.rootContent.contentSize = CGSizeMake(kScreenW, view.bottom + 18);
//    } else {
//        self.rootContent.contentSize = CGSizeMake(kScreenW, self.serviceView.bottom + 18);
//    }
}

/// 预约时间
- (void)initWithAppointTimeView {
    if (!_appointTimeView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kWDAppointTimeViewHeight);
        _appointTimeView = [[WDAppointTimeView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_appointTimeView];
    }
    if (self.viewModel.taskDetailModel) {
        [self.appointTimeView updateWithViewData:self.viewModel.taskDetailModel];
    }
}

/// 类型状态
- (void)initWithTypeFlowView {
    if (!_typeFlowView) {
        CGRect frame = CGRectMake(0, self.appointTimeView.bottom, kScreenW, kWDTaskTypeServFlowViewHeight);
        _typeFlowView = [[WDTaskTypeServFlowView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_typeFlowView];
    }
    if (self.viewModel.taskDetailModel) {
        [self.typeFlowView updateWithViewData:self.viewModel.taskDetailModel];
    }
}

- (void)initWithBasicSectionView {
    if (!_basicSectionView) {
        CGRect frame = CGRectMake(0, self.typeFlowView.bottom, kScreenW, kWDTaskDetailSectionViewHeight);
        _basicSectionView = [[WDTaskDetailSectionView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_basicSectionView];
        [self.basicSectionView updateWithViewData:@"基本信息"];
    }
}

/// 基本信息
- (void)initWithBasicInfoView {
    if (!_basicInfoView) {
        
        int rows = ceilf(self.viewModel.taskDetailModel.problemPhotoList.count / 3.0);
        float imgViewHeight = 0;
        if (self.viewModel.taskDetailModel.problemPhotoList.count > 0) {
            imgViewHeight = rows * 80 + 5 * (rows - 1) - 80;
        }
        
        
//        float height = kWDTaskDetailBasicSingleViewHeight * 12 + (kWDTaskDetailBasicSingleViewHeight - 18 + self.viewModel.taskDetailModel.vehicleAdsHeight) + 14 + kWDTaskDetailBasicSingleImageViewHeight + imgViewHeight + 14;
        
        float height = kWDTaskDetailBasicSingleViewHeight * 11 + 14 + kWDTaskDetailBasicSingleImageViewHeight + imgViewHeight + 14;
        
        if (self.viewModel.taskDetailModel && self.viewModel.taskDetailModel.detailHeight > 10) {
            height += (self.viewModel.taskDetailModel.detailHeight - 18);
        }
        CGRect frame = CGRectMake(0, self.basicSectionView.bottom, kScreenW, height);
        _basicInfoView = [[WDWorderOrdMainVehDetailBasicView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_basicInfoView];
    }
    if (self.viewModel.taskDetailModel) {
        [self.basicInfoView updateWithViewData:self.viewModel.taskDetailModel];
    }
}

/// 车辆信息
- (void)initWithCarSectionView {
    if (!_carSectionView) {
        CGRect frame = CGRectMake(0, self.basicInfoView.bottom, kScreenW, kWDTaskDetailSectionViewHeight);
        _carSectionView = [[WDTaskDetailSectionView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_carSectionView];
        [self.carSectionView updateWithViewData:@"车辆信息"];
    }
}

/// 车辆信息
- (void)initWithCarInfoView {
    if (!_carInfoView) {
        float height = kWDTaskDetailBasicSingleViewHeight * 2 + 14;
        height = height + self.viewModel.taskDetailModel.batteryList.count * 2 * kWDTaskDetailBasicSingleViewHeight;
        height = height + self.viewModel.taskDetailModel.canFaultInfoList.count * kWDTaskDetailBasicSingleViewHeight - kWDTaskDetailBasicSingleViewHeight;
        CGRect frame = CGRectMake(0, self.carSectionView.bottom, kScreenW, height);
        _carInfoView = [[WDWorderOrdMainVehInfoDetailView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_carInfoView];
    }
    if (self.viewModel.taskDetailModel) {
        [self.carInfoView updateWithViewData:self.viewModel.taskDetailModel];
    }
}

/// 服务流程
- (void)initWithServiceSectionView {
    if (!_serviceSectionView) {
        CGRect frame = CGRectMake(0, self.carInfoView.bottom, kScreenW, kWDTaskDetailSectionViewHeight);
        _serviceSectionView = [[WDTaskDetailSectionView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_serviceSectionView];
        [self.serviceSectionView updateWithViewData:@"服务流程"];
    }
}

- (void)initWithServiceView {
    if (!_serviceView) {
        float imageWidth = (kScreenW - 20 * 2 - 16 * 2) / 3.0;
        float imageHeight = imageWidth / 1.42857;
        float imageTitleHeight = imageHeight + 5 + 18;
        float height = kWDTaskDetailBasicSingleViewHeight * 4 + 14;
        
        if (self.viewModel.taskDetailModel.inspectFlowModel) {
            height = height + kWDTaskDetailBasicSingleViewHeight * 3 - 10 + self.viewModel.taskDetailModel.inspectFlowModel.workExplainHeight + 14 + 18 + 12 +  imageTitleHeight * 2 + 20 + self.viewModel.taskDetailModel.inspectFlowModel.otherFilesHeight + kWDTaskDetailBasicSingleViewHeight * 2;
        }
        
        CGRect frame = CGRectMake(0, self.serviceSectionView.bottom, kScreenW, height);
        _serviceView = [[WDWorderOrdMainVehDetailServiceView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_serviceView];
    }
    if (self.viewModel.taskDetailModel) {
        [self.serviceView updateWithViewData:self.viewModel.taskDetailModel];
    }
}

- (void)initWithMainFlowViews {
    CGRect frame = CGRectMake(0, self.serviceView.bottom, kScreenW, 0);
    NSArray *list = self.viewModel.taskDetailModel.tmpTaskFlows;
    WDWorderOrdMainVehDetailFlowView *preView = nil;
    for (int i=0; i<list.count; i++) {
        WDTaskFlowModel *model = list[i];
        frame.size.height = model.flowHeight;
        if (preView) {
            frame.origin.y = preView.bottom;
        }
        WDWorderOrdMainVehDetailFlowView *view = [[WDWorderOrdMainVehDetailFlowView alloc] initWithFrame:frame];
        [self.rootContent addSubview:view];
        [view updateWithViewData:model];
        preView = view;
        [self.viewList addObject:view];
    }
}

/// 取消流程
- (void)initWithServiceCancelView {
    if (!self.viewModel.taskDetailModel.cancel) {
        return;
    }
    if (!_serviceCancelView) {
        float height = kWDTaskDetailBasicSingleViewHeight * 3;
        if (self.viewModel.taskDetailModel.cancel.source == 0) {
            height = kWDTaskDetailBasicSingleViewHeight * 2;
        }
        height = height - 18 + self.viewModel.taskDetailModel.cancel.contentHeight;
        
        float bottom = 0;
        if (self.viewModel.taskDetailModel.tmpTaskFlows.count > 0) {
            WDWorderOrdMainVehDetailFlowView *view = self.viewList.lastObject;
            bottom = view.bottom;
        } else {
            bottom = self.serviceView.bottom;
        }
        
        CGRect frame = CGRectMake(0, bottom, kScreenW, height);
        _serviceCancelView = [[WDGoutServiceCancelView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_serviceCancelView];
    }
    if (self.viewModel.taskDetailModel) {
        [_serviceCancelView updateWithViewData:self.viewModel.taskDetailModel];
    }
}

- (void)initWithServiceFinishView {
//    servOperate
    WDTaskFlowModel *model = self.viewModel.taskDetailModel.taskFlowList.lastObject;
    if (self.viewModel.taskDetailModel.servFlow == 5 && model && model.servOperate == 6) {
        
    } else {
        return;
    }
    if (!_serviceFinishView) {
        float height = kWDTaskDetailBasicSingleViewHeight * 2;
        float bottom = 0;
        if (self.viewModel.taskDetailModel.tmpTaskFlows.count > 0) {
            WDWorderOrdMainVehDetailFlowView *view = self.viewList.lastObject;
            bottom = view.bottom;
        } else {
            bottom = self.serviceView.bottom;
        }
        
        CGRect frame = CGRectMake(0, bottom, kScreenW, height);
        _serviceFinishView = [[WDServiceFinishView alloc] initWithFrame:frame];
        [self.rootContent addSubview:_serviceFinishView];
    }
    
    if (self.viewModel.taskDetailModel) {
        [_serviceFinishView updateWithViewData:self.viewModel.taskDetailModel];
    }
}

- (void)fromServerRequestData {
    
    @weakify(self);
    [self.viewModel requestWithTaskInfoDetail:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [self updateWithUI];
        } else {
            [SVProgressHUD showInfoWithStatus:@"获取详情失败，请稍后再试！"];
        }
    }];
}

@end
